package com.winter.service;

import com.google.common.base.Strings;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.winter.mapper.DatumDownloadMapper;
import com.winter.mapper.IAttaMapper;
import com.winter.mapper.NewDatumOrderMapper;
import com.winter.model.DatumDownload;
import com.winter.model.IAtta;
import com.winter.model.NewDatumOrder;
import com.winter.util.Constants;
import com.winter.util.P2pPubParaConstants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CoursewareSerivce {

    @Autowired
    private DatumDownloadMapper datumDownloadMapper;

    @Autowired
    private IAttaMapper iAttaMapper;

    @Autowired
    private NewDatumOrderMapper newDatumOrderMapper;

    /**
     * 课件列表
     */
    public PageInfo list(Integer pageSize, Integer pageNumber, String key, String userNo, String userType, String type) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        List<DatumDownload> list = datumDownloadMapper.list(key, type);

        if (list.size() > 0) {
            list.forEach(f -> {
                // 判断用户是否属于什么类型
                if (userType != null) {
                    if (!Strings.isNullOrEmpty(f.getUserviptype())) {
                        if (f.getUserviptype()
                             .contains(userType)) {
                            f.setPrice("0");
                        }
                    }
                }
                if (!Strings.isNullOrEmpty(userNo)) {
                    NewDatumOrder newDatumOrder = newDatumOrderMapper.queryUserNoAndDatumId(userNo, f.getId() + "");
                    if (newDatumOrder != null) {
                        f.setIsPay("1");
                    } else {
                        f.setIsPay("0");
                    }
                } else {
                    f.setIsPay("0");
                }
                List<IAtta> iAtta = iAttaMapper.getIAtta(f.getId()
                                                          .longValue(), P2pPubParaConstants.AttaType.COURSEWARE_DOWNLOAD);
                Integer size = 0;
                if (iAtta.size() > 0) {
                    for (IAtta atta : iAtta) {
                        if (atta.getAttaSize() != null) {
                            Integer attaSize = atta.getAttaSize();
                            size = size + attaSize;
                        }
                    }
                }
                // 文件大小
                if (size > 0) {
                    f.setDatumSizo((size / 1024));
                } else {
                    f.setDatumSizo(size);
                }
                // 购买了 就返回资料列表 否则不进行返回
                if (f.getIsPay()
                     .equals("1") || f.getPrice()
                                      .equals("0")) {
                    f.setiAttas(iAtta);
                } else {
                    f.setiAttas(null);
                }

            });

        }
        PageInfo<DatumDownload> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
